/**
 * Author: xing
 * Date: 2016.3.18
 * 这个脚本的主要作用在于建立右键菜单
 */


/**
 * [jp 扩展string类，使之具有连接路径的函数
 * 1) 除非明确使用第二个参数，且为true，否则不会加'\''
 * 2) 对于registry的三个操作函数regread,readwrite, regdelete，加斜线代表向，不加代表属性]
 * @param  {[string]} add_path [被添加的后缀]
 * @param  {[bool]} [slash_flag] [是否添加斜线, false和不写这个参数，代表加斜线，否则不加斜线]
 * @return {[string]} [返回拼接后的字符串]
 */
String.prototype.jp = function(add_path, slash_flag){
	var add_slash_flag = arguments[1] ? arguments[1] : false;
	var slash = add_slash_flag ? '' : '\\';
	if(this.slice(-1) == '\\'){
		return this + add_path + slash;
	}else{
		return this + '\\' + add_path + slash;
	}
}


/**
 * [ehco Alert object]
 * @param  {[string]} object [object to be alert]
 * @return {[void]}        [none]
 */
function alert(object) {
	WScript.Echo(object);
}



/**
 * [backgroundMenu 设置背景菜单]
 * @param  {[ActiveXObject]} shell [The object of wscript.shell]
 * @param  {[object]} [writeObject] [background object]
 * @return {[void]}       [none]
 */
function writeMenu(shell, writeObject) {
	var menus = writeObject.menus;
	var sbcommands = new Array();

	for (var i = 0; i < menus.length; i++) {
		var item = menus[i];
		var kname = item.name;
		sbcommands.push(kname);
		try{
			// 如果存在该键，就删了它，如果不存在就会出现读取错误
			var tv = shell.regread(writeObject.commandStorePath.jp(kname));
			//shell.regdelete(writeObject.commandStorePath.jp(kname));
			shell.Run('reg delete ' + writeObject.commandStorePath.jp(kname) + ' /f', 0, false);
		}
		catch(e){
			alert(e.message);
		}finally{
			// 添加项以及默认值
			shell.regwrite(writeObject.commandStorePath.jp(kname), kname, 'REG_SZ');
			// 判断并添加icon
			if(item.icon){
				shell.regwrite(writeObject.commandStorePath.jp(kname).jp('Icon', true), item.icon, 'REG_SZ');
			}
			// 判断并添加verb
			if(item.verb){
				shell.regwrite(writeObject.commandStorePath.jp(kname).jp('MUIVerb', true), item.verb, 'REG_SZ');
			}
			// 判断并添加description
			if(item.description){
				shell.regwrite(writeObject.commandStorePath.jp(kname).jp('Description', true), item.verb, 'REG_SZ');
			}
			// 判断并添加handler
			if(item.handler){
				shell.regwrite(writeObject.commandStorePath.jp(kname).jp('CommandStateHandler', true), item.verb, 'REG_SZ');
			}
			
			// 添加commands项以及默认值
			if(item.handler){
				shell.regwrite(writeObject.commandStorePath.jp(kname).jp('Command'), '');
				shell.regwrite(writeObject.commandStorePath.jp(kname).jp('Command').jp('DelegateExecute', true), item.command, 'REG_SZ');	
			}else{
				shell.regwrite(writeObject.commandStorePath.jp(kname).jp('Command'), item.command, 'REG_SZ');
			}
			
		}
	}

	/*默认属性的值如何设置，非常重要*/
	// 添加背景菜单
	try{
		var tv = shell.regread(writeObject.directoryPath.jp('ct'));
		shell.Run('reg delete ' + writeObject.directoryPath.jp('ct') + ' /f', 0, false);
	}catch(e){
		alert(e.message);
	}
	shell.regwrite(writeObject.directoryPath.jp('ct'), 'ct');
	shell.regwrite(writeObject.directoryPath.jp('ct').jp('Icon', true), writeObject.icon, 'REG_SZ');
	shell.regwrite(writeObject.directoryPath.jp('ct').jp('MUIVerb', true), writeObject.verb, 'REG_SZ');
	shell.regwrite(writeObject.directoryPath.jp('ct').jp('SubCommands', true), sbcommands.join(';'), 'REG_SZ');
	
}


/**
 * [bgObject 背景右键菜单]
 * @type {Object}
 */
var bgObject = {
	directoryPath: 'HKCR\\directory\\background\\shell\\',
	commandStorePath: 'HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\',
	verb: 'Command Tools',
	icon: 'Wscript.exe',
	menus: new Array(
		{
			name: 'Xing.gitbash',
			icon: '"C:\\Program Files\\Git\\git-bash.exe"',
			verb: 'Git bash',
			command: '"C:\\Program Files\\Git\\git-bash.exe"'
		},
		{
			name: 'Xing.shfile',
			icon: 'Wscript.exe',
			verb: 'SH file',
			command: 'Wscript.exe //nologo E:\\Documents\\GitProjects\\ShellTools\\Windows\\SHFile.js'
		},
		{
			name: 'Xing.shfileextension',
			icon: 'Wscript.exe',
			verb: 'SH file extension',
			command: 'Wscript.exe //nologo E:\\Documents\\GitProjects\\ShellTools\\Windows\\SHFileExtension.js'
		},
		{
			name: 'Xing.opensb',
			icon: '"D:\\Program Files\\Sublime Text 3\\sublime_text.exe"',
			verb: 'Open sublime text',
			command: '"D:\\Program Files\\Sublime Text 3\\sublime_text.exe"'
		},
		{
			name: 'Xing.cmd',
			icon: 'cmd.exe',
			verb: 'Open command prompt',
			command: 'cmd.exe'
		}
	)
};

/**
 * [fileObject 文件右键菜单]
 * @type {Object}
 */
var fileObject = {
	directoryPath: 'HKCR\\*\\shell\\',
	commandStorePath: 'HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\CommandStore\\shell\\',
	verb: 'Command Tools',
	icon: 'Wscript.exe',
	menus: new Array(
		{
			name: 'Xing.openwithsb',
			icon: '"D:\\Program Files\\Sublime Text 3\\sublime_text.exe"',
			verb: 'Open with sublime text',
			command: '"D:\\Program Files\\Sublime Text 3\\sublime_text.exe" "%1"'
		},
		{
			name: 'Xing.copypath',
			icon: 'Wscript.exe',
			verb: 'Copy Path',
			command: 'Wscript.exe //nologo E:\\Documents\\GitProjects\\ShellTools\\Windows\\ClipBoard.js "%1"'
		}
	)
}


var shell = new ActiveXObject('WScript.shell');
writeMenu(shell, bgObject);
//writeMenu(shell, fileObject)
alert('Task is finished...');